/**
 * @file STL_HttpTools.cpp
 * @author Derek Liu (yjkhtddx@sina.com)
 * @brief 
 * @version 0.1
 * @date 2021-02-19
 * 
 * @copyright Copyright Sci-Tech Laboratories (c) 2021
 * 
 */

#include <string.h>
#include "STL_Path.h"
#include "STL_HttpTools.h"

namespace STL
{
    STL_HttpTools::STL_HttpTools()
    {
        {
            m_StatusCode_table.clear();
            m_StatusCode_table[100] = "Continue";
            m_StatusCode_table[101] = "Switching Protocol";
            m_StatusCode_table[102] = "Processing";
            m_StatusCode_table[103] = "Early Hints";
            m_StatusCode_table[200] = "OK";
            m_StatusCode_table[201] = "Created";
            m_StatusCode_table[202] = "Accepted";
            m_StatusCode_table[203] = "Non-Authoritative Information";
            m_StatusCode_table[204] = "No Content";
            m_StatusCode_table[205] = "Reset Content";
            m_StatusCode_table[206] = "Partial Content";
            m_StatusCode_table[207] = "Multi-Status";
            m_StatusCode_table[208] = "Already Reported";
            m_StatusCode_table[226] = "IM Used";
            m_StatusCode_table[300] = "Multiple Choice";
            m_StatusCode_table[301] = "Moved Permanently";
            m_StatusCode_table[302] = "Found";
            m_StatusCode_table[303] = "See Other";
            m_StatusCode_table[304] = "Not Modified";
            m_StatusCode_table[305] = "Use Proxy";
            m_StatusCode_table[306] = "unused";
            m_StatusCode_table[307] = "Temporary Redirect";
            m_StatusCode_table[308] = "Permanent Redirect";
            m_StatusCode_table[400] = "Bad Request";
            m_StatusCode_table[401] = "Unauthorized";
            m_StatusCode_table[402] = "Payment Required";
            m_StatusCode_table[403] = "Forbidden";
            m_StatusCode_table[404] = "Not Found";
            m_StatusCode_table[405] = "Method Not Allowed";
            m_StatusCode_table[406] = "Not Acceptable";
            m_StatusCode_table[407] = "Proxy Authentication Required";
            m_StatusCode_table[408] = "Request Timeout";
            m_StatusCode_table[409] = "Conflict";
            m_StatusCode_table[410] = "Gone";
            m_StatusCode_table[411] = "Length Required";
            m_StatusCode_table[412] = "Precondition Failed";
            m_StatusCode_table[413] = "Payload Too Large";
            m_StatusCode_table[414] = "URI Too Long";
            m_StatusCode_table[415] = "Unsupported Media Type";
            m_StatusCode_table[416] = "Range Not Satisfiable";
            m_StatusCode_table[417] = "Expectation Failed";
            m_StatusCode_table[418] = "I'm a teapot";
            m_StatusCode_table[421] = "Misdirected Request";
            m_StatusCode_table[422] = "Unprocessable Entity";
            m_StatusCode_table[423] = "Locked";
            m_StatusCode_table[424] = "Failed Dependency";
            m_StatusCode_table[425] = "Too Early";
            m_StatusCode_table[426] = "Upgrade Required";
            m_StatusCode_table[428] = "Precondition Required";
            m_StatusCode_table[429] = "Too Many Requests";
            m_StatusCode_table[431] = "Request Header Fields Too Large";
            m_StatusCode_table[451] = "Unavailable For Legal Reasons";
            m_StatusCode_table[500] = "Internal Server Error";
            m_StatusCode_table[501] = "Not Implemented";
            m_StatusCode_table[502] = "Bad Gateway";
            m_StatusCode_table[503] = "Service Unavailable";
            m_StatusCode_table[504] = "Gateway Timeout";
            m_StatusCode_table[505] = "HTTP Version Not Supported";
            m_StatusCode_table[506] = "Variant Also Negotiates";
            m_StatusCode_table[507] = "Insufficient Storage";
            m_StatusCode_table[508] = "Loop Detected";
            m_StatusCode_table[510] = "Not Extended";
            m_StatusCode_table[511] = "Network Authentication Required";
        }
        {
            m_ContentType_table.clear();
            m_ContentType_table["html"] = "text/html";
            m_ContentType_table["htm"] = "text/html";
            m_ContentType_table["shtml"] = "text/html";
            m_ContentType_table["css"] = "text/css";
            m_ContentType_table["xml"] = "text/xml";
            m_ContentType_table["gif"] = "image/gif";
            m_ContentType_table["jpeg"] = "image/jpeg";
            m_ContentType_table["jpg"] = "image/jpeg";
            m_ContentType_table["js"] = "application/javascript";
            m_ContentType_table["map"] = "application/javascript";
            m_ContentType_table["atom"] = "application/atom+xml";
            m_ContentType_table["rss"] = "application/rss+xml";
            m_ContentType_table["mml"] = "text/mathml";
            m_ContentType_table["txt"] = "text/plain";
            m_ContentType_table["jad"] = "text/vnd.sun.j2me.app-descriptor";
            m_ContentType_table["wml"] = "text/vnd.wap.wml";
            m_ContentType_table["htc"] = "text/x-component";
            m_ContentType_table["png"] = "image/png";
            m_ContentType_table["tif"] = "image/tiff";
            m_ContentType_table["tiff"] = "image/tiff";
            m_ContentType_table["wbmp"] = "image/vnd.wap.wbmp";
            m_ContentType_table["ico"] = "image/x-icon";
            m_ContentType_table["jng"] = "image/x-jng";
            m_ContentType_table["bmp"] = "image/x-ms-bmp";
            m_ContentType_table["svg"] = "image/svg+xml";
            m_ContentType_table["svgz"] = "image/svg+xml";
            m_ContentType_table["webp"] = "image/webp";
            m_ContentType_table["woff"] = "application/font-woff";
            m_ContentType_table["woff2"] = "application/font-woff";
            m_ContentType_table["jar"] = "application/java-archive";
            m_ContentType_table["war"] = "application/java-archive";
            m_ContentType_table["ear"] = "application/java-archive";
            m_ContentType_table["json"] = "application/json";
            m_ContentType_table["hqx"] = "application/mac-binhex40";
            m_ContentType_table["doc"] = "application/msword";
            m_ContentType_table["pdf"] = "application/pdf";
            m_ContentType_table["ps"] = "application/postscript";
            m_ContentType_table["eps"] = "application/postscript";
            m_ContentType_table["ai"] = "application/postscript";
            m_ContentType_table["rtf"] = "application/rtf";
            m_ContentType_table["m3u8"] = "application/vnd.apple.mpegurl";
            m_ContentType_table["xls"] = "application/vnd.ms-excel";
            m_ContentType_table["eot"] = "application/vnd.ms-fontobject";
            m_ContentType_table["ppt"] = "application/vnd.ms-powerpoint";
            m_ContentType_table["wmlc"] = "application/vnd.wap.wmlc";
            m_ContentType_table["kml"] = "application/vnd.google-earth.kml+xml";
            m_ContentType_table["kmz"] = "application/vnd.google-earth.kmz";
            m_ContentType_table["7z"] = "application/x-7z-compressed";
            m_ContentType_table["cco"] = "application/x-cocoa";
            m_ContentType_table["jardiff"] = "application/x-java-archive-diff";
            m_ContentType_table["jnlp"] = "application/x-java-jnlp-file";
            m_ContentType_table["run"] = "application/x-makeself";
            m_ContentType_table["pl"] = "application/x-perl";
            m_ContentType_table["pm"] = "application/x-perl";
            m_ContentType_table["prc"] = "application/x-pilot";
            m_ContentType_table["pdb"] = "application/x-pilot";
            m_ContentType_table["rar"] = "application/x-rar-compressed";
            m_ContentType_table["rpm"] = "application/x-redhat-package-manager";
            m_ContentType_table["sea"] = "application/x-sea";
            m_ContentType_table["swf"] = "application/x-shockwave-flash";
            m_ContentType_table["sit"] = "application/x-stuffit";
            m_ContentType_table["tcl"] = "application/x-tcl";
            m_ContentType_table["tk"] = "application/x-tcl";
            m_ContentType_table["der"] = "application/x-x509-ca-cert";
            m_ContentType_table["pem"] = "application/x-x509-ca-cert";
            m_ContentType_table["crt"] = "application/x-x509-ca-cert";
            m_ContentType_table["xpi"] = "application/x-xpinstall";
            m_ContentType_table["xhtml"] = "application/xhtml+xml";
            m_ContentType_table["xspf"] = "application/xspf+xml";
            m_ContentType_table["zip"] = "application/zip";
            m_ContentType_table["bin"] = "application/octet-stream";
            m_ContentType_table["exe"] = "application/octet-stream";
            m_ContentType_table["dll"] = "application/octet-stream";
            m_ContentType_table["deb"] = "application/octet-stream";
            m_ContentType_table["dmg"] = "application/octet-stream";
            m_ContentType_table["iso"] = "application/octet-stream";
            m_ContentType_table["img"] = "application/octet-stream";
            m_ContentType_table["msi"] = "application/octet-stream";
            m_ContentType_table["msp"] = "application/octet-stream";
            m_ContentType_table["msm"] = "application/octet-stream";
            m_ContentType_table["docx"] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            m_ContentType_table["xlsx"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            m_ContentType_table["pptx"] = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
            m_ContentType_table["mid"] = "audio/midi";
            m_ContentType_table["midi"] = "audio/midi";
            m_ContentType_table["kar"] = "audio/midi";
            m_ContentType_table["mp3"] = "audio/mpeg";
            m_ContentType_table["ogg"] = "audio/ogg";
            m_ContentType_table["m4a"] = "audio/x-m4a";
            m_ContentType_table["ra"] = "audio/x-realaudio";
            m_ContentType_table["3gpp"] = "video/3gpp";
            m_ContentType_table["3gp"] = "video/3gpp";
            m_ContentType_table["ts"] = "video/mp2t";
            m_ContentType_table["mp4"] = "video/mp4";
            m_ContentType_table["mpeg"] = "video/mpeg";
            m_ContentType_table["mpg"] = "video/mpeg";
            m_ContentType_table["mov"] = "video/quicktime";
            m_ContentType_table["webm"] = "video/webm";
            m_ContentType_table["flv"] = "video/x-flv";
            m_ContentType_table["m4v"] = "video/x-m4v";
            m_ContentType_table["mng"] = "video/x-mng";
            m_ContentType_table["asx"] = "video/x-ms-asf";
            m_ContentType_table["asf"] = "video/x-ms-asf";
            m_ContentType_table["wmv"] = "video/x-ms-wmv";
            m_ContentType_table["avi"] = "video/x-msvideo";
        }
    }

    STL_HttpTools *STL_HttpTools::getInstance()
    {
        static STL_HttpTools instance;
        return &instance;
    }

    std::string STL_HttpTools::getHttpContentType(std::string path)
    {
        std::string ext = STL_Path::extname(path);
        if (ext.empty() || STL_HttpTools::getInstance()->m_ContentType_table.count(ext) != 1)
        {
            return "text/plain";
        }
        else
        {
            return STL_HttpTools::getInstance()->m_ContentType_table[ext];
        }
    }

    std::string STL_HttpTools::getHttpCodePhrase(int statusCode)
    {
        if (STL_HttpTools::getInstance()->m_StatusCode_table.count(statusCode) != 1)
        {
            return "Unknown";
        }
        else
        {
            return STL_HttpTools::getInstance()->m_StatusCode_table[statusCode];
        }
    }

} //namespace mediakit